/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.controller.util;

import com.googlecode.bambole.util.SupportedLocalesUtil;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Named("localizationUtil")
@SessionScoped
public class LocalizationUtil implements Serializable {

    private Locale current;
    private Map<String, Object> items;

    public Locale getSelected() {
        return current == null ? ConfigurationUtil.getDefaultLocale() : current;
    }

    public void setSelected(Locale current) {
        this.current = current;
    }

    public Map<String, Object> getItems() {
        return items;
    }

    public void setItems(Map<String, Object> items) {
        this.items = items;
    }

    public static String getSearchDateFormatPattern() {
        switch (JsfUtil.getLocale().toLanguageTag()) {
            case "en-US":
                return new SimpleDateFormat("MM/dd/yyyy", Locale.forLanguageTag("en-US")).toPattern();
            case "pt-BR":
                return new SimpleDateFormat("dd/MM/yyyy", Locale.forLanguageTag("pt-BR")).toPattern();
            default:
                return new SimpleDateFormat("dd/MM/yyyy", Locale.forLanguageTag("pt-BR")).toPattern();
        }
    }

    public static String getDateFormatPattern() {
        switch (JsfUtil.getLocale().toLanguageTag()) {
            case "en-US":
                return new SimpleDateFormat("MMM dd, yyyy", Locale.forLanguageTag("en-US")).toPattern();
            case "pt-BR":
                return new SimpleDateFormat("dd/MMM/yyyy", Locale.forLanguageTag("pt-BR")).toPattern();
            default:
                return new SimpleDateFormat("dd/MMM/yyyy", Locale.forLanguageTag("pt-BR")).toPattern();
        }
    }

    public String getDateFormatMediumPattern() {
        return getDateFormatPattern();
    }

    public static String getDateTimeFormatPattern() {
        switch (JsfUtil.getLocale().toLanguageTag()) {
            case "en-US":
                return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.forLanguageTag("en-US")).toPattern();
            case "pt-BR":
                return new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss", Locale.forLanguageTag("pt-BR")).toPattern();
            default:
                return new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss", Locale.forLanguageTag("pt-BR")).toPattern();
        }
    }

    public String getDateTimeFormatMediumPattern() {
        return getDateTimeFormatPattern();
    }

    public String getToday() {
        return new SimpleDateFormat(getDateFormatMediumPattern()).format(new Date());
    }

    public TimeZone getDefaultTimeZone() {
        return TimeZone.getDefault();
    }

    public void updateSelection() {
        setSelected(current);
    }

    public SelectItem[] getItemsAvailableSelectOne() {

        SelectItem[] locales = new SelectItem[items.size() + 1];

        locales[0] = new SelectItem(null, null);

        int i = 1;
        for (Map.Entry<String, Object> entry : items.entrySet()) {
            locales[i++] = new SelectItem(entry.getValue(), entry.getKey());
        }

        return locales;
    }

    @PostConstruct
    public void init() {
        current = getSelected();

        setItems(SupportedLocalesUtil.getItems());
    }
}
